﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SM2Utilities;
using SM2Profile;
using SM2SetupOptions;
using SM2ReportsSentiment;
using SM2SetupSearch;

namespace SM2RegressionBugVerificationV5
{
    public class ID1433
    {
        public static void run(string strSelectedProfile)
        {
            //Step1. Load Profiles
            CommonLoader.load("home.aspx");

            //Step2. Select profile
            ProfileActions.loadProfile(strSelectedProfile);

            //Step3. Verify categories match with the setting expectation
            checkDictionary(strSelectedProfile);

            //Create the search for every single entry of each category
            //checkSentiment(strSelectedProfile);
        }

        public static void checkDictionary(string strSelectedProfile)
        {
            //Compare
            Boolean blMatch = true;
            string strListCate = Repository.ID1433.getListOfCategoies();
            string[] list = strListCate.Split(',');
            for (int i = 0 ; i < list.Length; i++)
            {
                string strCat = list[i].Trim();
                Boolean blMatch_Cate = true;
                if (!strCat.Equals(""))
                {
                    //Get list of entry items from SM2
                    string strListEntriesSM2 = Dictionary.GetDictionaryEntries(strSelectedProfile, "Greek", strCat);

                    //Read list of entry items from the file
                    string strListItemsFile = Repository.ID1433.getItemsOfCate(strCat);

                    //Log list of entry items from SM2
                    //string file1 = Utilities.GlobalFolder.REPOSITORY_LOG_CURRENTLOG + strCat+ "_EntriesFromSM2.csv";
                    //Utilities.GlobalObject.LOGGER.write2File(strListEntriesSM2, file1);

                    //Log list of entry items from SM2
                    //string file2 = Utilities.GlobalFolder.REPOSITORY_LOG_CURRENTLOG + strCat + "_EntriesFromFile.csv";
                    //Utilities.GlobalObject.LOGGER.write2File(strListItemsFile, file2);

                    //Validation
                    string strNON_MATCH = "FILE<>SM2";
                    string[] listEntriesFromSM2 = strListEntriesSM2.Split(','); int intLength_listEntriesFromSM2 = listEntriesFromSM2.Length;
                    string[] listEntriesFromFile = strListItemsFile.Split(','); int intLength_listEntriesFromFile = listEntriesFromFile.Length;
                    for (int iFile = 0; iFile < intLength_listEntriesFromFile; iFile++)
                    {
                        string strEntryFile = listEntriesFromFile[iFile];
                        string strEntrySM2 = "BLANK";
                        if (intLength_listEntriesFromSM2 > iFile)
                        {
                            strEntrySM2 = listEntriesFromSM2[iFile];
                        }

                        //Compare
                        /* They are sorted Ascending as Case Sensitive (which should not matter because they said they are all lower case).
                         * In Dictionary page they are shown ascending (from A to Z), so they should look the same.
                         * Ariel Trevisan <arielt@argeniss.com>
                        */
                        if (!strEntryFile.Equals(""))
                        {
                            if (!strEntryFile.Equals(strEntrySM2))
                            {
                                strNON_MATCH += "," + strEntryFile + "<>" + strEntrySM2;
                                blMatch = false;
                                blMatch_Cate = false;
                            }
                        }
                    }

                    //Log list of non-match entry items from SM2 vs FILE if there is
                    if (!blMatch_Cate)
                    {
                        string file3 = Utilities.GlobalFolder.REPOSITORY_LOG_CURRENTLOG + strCat + "_NOT_MATCH.csv";
                        Utilities.GlobalObject.LOGGER.write2File(strNON_MATCH, file3);
                    }
                }
            }

            //Throw exception if there is one non-match
            if (!blMatch) throw new Exception("Some item(s) do(es) not match");
        }

        public static void checkSentiment(string strSelectedProfile)
        {
            Boolean blSearch = true;
            string strListCate_Search = Repository.ID1433.getListOfCategoies();
            string[] listCate_Search = strListCate_Search.Split(',');

            //DEBUG HERE
            for (int i = 0; i < listCate_Search.Length; i++)
            {
                string strCateItem = listCate_Search[i].Trim();
                if (!strCateItem.Equals(""))
                {
                    //Read list of entry items from the file
                    string strListItemsFile_Search = Repository.ID1433.getItemsOfCate(strCateItem);
                    string[] listEntriesFromFile_Search = strListItemsFile_Search.Split(','); int intLength_listEntriesFromFile_Search = strListItemsFile_Search.Length;

                    //Pick top 20 keywords of sentiment category for making the search.
                    //int intLoop = (intLength_listEntriesFromFile_Search > 1)? 1 : intLength_listEntriesFromFile_Search;
                    //for (int iFile = 0; iFile < intLoop; iFile++)
                    //{
                    Boolean blReasonableNumber = true;
                    string strNON_MATCH_NUMBER = "FOUND RESULT<>SENTIMENTED RESULT";
                    string strEntryFile_Search = listEntriesFromFile_Search[59]; //PICK THE #59
                    if (!strEntryFile_Search.Equals(""))
                    {

                        if (!SearchCommonActions.wasExisted(strSelectedProfile, strEntryFile_Search))
                        {
                            //Create a search
                            SM2SetupSearch.Search.NewSearchManualEntry(strSelectedProfile, strEntryFile_Search);

                            //Validate it was run completely
                            CommonLoader.load("jobs.aspx");
                            SM2SetupJobs.RunningCompleted.checkSearchCompleted(strEntryFile_Search);
                        }

                        ////Disable
                        //SM2SetupSearch.Search.SetDisableSearch(strSelectedProfile, strEntryFile_Search);

                        ////Check Result to make sure we get some - then disable it
                        CommonLoader.load("scan_results.aspx");
                        int intFoundNumberResults = SM2ViewResults.ViewResultsActions.CountNumberOfResult(strEntryFile_Search);

                        if (intFoundNumberResults > 0)
                        {
                            //Loop over 10 first results to make sure the content emotion sentiment works
                            CommonLoader.load("sentiment_emotion.aspx");
                            int intLoopSentimentResult = (intFoundNumberResults > 10) ? 10 : intFoundNumberResults;
                            for (int iSentiment = 1; iSentiment <= intLoopSentimentResult; iSentiment++)
                            {
                                //Check Content Emotion
                                int intFoundNumberSentiment = ReportSentimentActions.GetNumberFromAResult(strCateItem, strEntryFile_Search, iSentiment);

                                //Validate
                                if (intFoundNumberSentiment == 0)
                                {
                                    strNON_MATCH_NUMBER += ", Result [" + iSentiment + "] = " + intFoundNumberSentiment;
                                    blSearch = false;
                                    blReasonableNumber = false;
                                }
                            }

                            //Log list of non-match entry items from SM2 vs FILE if there is
                            if (!blReasonableNumber)
                            {
                                string file3 = Utilities.GlobalFolder.REPOSITORY_LOG_CURRENTLOG + strCateItem + "_FOUND_NOT_REASONABLE_NUMBER.csv";
                                Utilities.GlobalObject.LOGGER.write2File(strNON_MATCH_NUMBER, file3);
                            }
                        }
                    }
                    //}
                }
            }

            //Throw exception if there is one search results > number of sentiment
            if (!blSearch) throw new Exception("search results > number of sentiment");
        }
    }
}
